﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Profile;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;
using Facade;
using System.Collections;
using System.Data;

namespace IronOne.SalesOne.SalesOneWeb.ContactManagement
{
    public partial class SearchContact : SOSecurityPage, IDataPage, IntTest
    {
        public static List<Branch> brnch;
        public static List<SalesOneUnit> un;
        public static List<string> so;
        private int pageNo = 1;


        protected void Page_Load(object sender, EventArgs e)
        {

            try
            {
                if (!IsPostBack)
                {
                    lblClientId.Text = "Client ID";
                    lblContId.Text = "Contract No";
                    txtClientId.Visible = true;
                    txtContractNo.Visible = true;
                    lblContId.Visible = true;
                    lblClientId.Visible = true;

                    CodeFacade coFac = new CodeFacade();
                    List<IDType> id = coFac.GetIDTypes("CodeId ASC");
                    IDType idSel = new IDType();


                    idSel.CodeId = -1;
                    idSel.Code = "Select...";
                    id.Insert(0, idSel);

                    drpIdType.DataSource = id;
                    drpIdType.DataTextField = "Code";
                    drpIdType.DataValueField = "CodeId";
                    drpIdType.DataBind();





                    FillDropDown("Branch");
                    FillDropDown("Unit");
                    FillDropDown("SO");

                    rdoContactType.SelectedValue = "1";
                }

                              
                
            }
            catch (Exception)
            {

            }
            
        }

        protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
        {

        }

        protected void TextBox3_TextChanged(object sender, EventArgs e)
        {

        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillDropDown("Unit");
            FillDropDown("SO");
        }

        protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            pageNo = 1;
            UCGrid1.SetText("1");
            

            if (rdoContactType.SelectedIndex != -1)
            {
                int contType = Convert.ToInt32(rdoContactType.SelectedValue);

                switch (contType)
                {
                    case 1:
                        //client
                        getContacts("Client",1);
                        break;
                    case 2:
                        //policy
                        getContacts("Policy",1);
                        break;
                    case 3:
                        //prospect
                        getContacts("Prospect",1);
                        break;
                    case 4:
                        //suspect
                        getContacts("Suspect",1);
                        break;
                    default:
                        break;
                }

                

            }

        }



        private void getContacts(string type,int pageNo)
        {
            try
            {
                if ((string.IsNullOrEmpty(txtIdNo.Text) && drpIdType.SelectedValue != "-1") || (!string.IsNullOrEmpty(txtIdNo.Text) && drpIdType.SelectedValue == "-1"))
                {
                    MassageSummery("IIS511");
                    //lblError.Text = "Please enter both ID Type and ID number";
                    return;
                }

                //try
                //{
                CustomerFacade cusFac = new CustomerFacade();
                StructureFacade sFac = new StructureFacade();

                string idNo = txtIdNo.Text;
                string clientId = string.Empty;
                string name = txtName.Text;
                string idType = "";

                if (!string.IsNullOrEmpty(txtClientId.Text))
                {
                    clientId = txtClientId.Text;
                }

                if (drpIdType.SelectedValue != "-1")
                {
                    idType = drpIdType.SelectedValue;
                }

                string contact = txtContractNo.Text;
                List<Branch> br = new List<Branch>();
                List<SalesOneUnit> unt = new List<SalesOneUnit>();
                List<string> soCode = new List<string>();


                if (branchDropDownList.SelectedItem.Text != "Select...")
                {
                    if (branchDropDownList.SelectedItem.Text == "Select All")
                    {
                        if (brnch.Count > 0)
                        {
                            if (brnch.ElementAt(0).BranchId == -1 && brnch.ElementAt(1).BranchId == -2)
                            {
                                brnch.RemoveRange(0, 2);

                            }

                            br = brnch;
                        }

                    }
                    else if (0 != branchDropDownList.SelectedValue.CompareTo("-1"))
                    {
                        br.Add(sFac.GetBranch(Convert.ToInt32(branchDropDownList.SelectedValue)));
                    }


                }

                if (unitDropDownList.SelectedItem.Text != "Select...")
                {
                    if (unitDropDownList.SelectedItem.Text == "Select All")
                    {
                        if (un.Count != 0)
                        {
                            if (un.ElementAt(0).UnitId == -1 && un.ElementAt(1).UnitId == -2)
                            {
                                un.RemoveRange(0, 2);
                            }
                        }


                        unt = un;
                    }
                    else if (0 != unitDropDownList.SelectedValue.CompareTo("-1"))
                    {
                        unt.Add(sFac.GetUnit(Convert.ToInt32(unitDropDownList.SelectedValue)));
                    }


                }

                if (soCodeDropDownList.SelectedItem.Text != "Select...")
                {
                    if (soCodeDropDownList.SelectedValue == "Select All")
                    {
                        if (so.Count != 0)
                        {
                            if (so.ElementAt(0).Contains("Select...") && so.ElementAt(1).Contains("Select All"))
                            {
                                so.RemoveRange(0, 2);
                            }


                        }

                        soCode = so;
                    }
                    else if (0 != soCodeDropDownList.SelectedValue.CompareTo("Select..."))
                    {
                        soCode.Add(soCodeDropDownList.SelectedValue);
                    }


                }



                ContactSearch sPol = new ContactSearch();
                sPol.IdNo = idNo;



                sPol.IdType = idType;
                if (br.Count == 0)
                {
                    br = null;
                }

                if (unt.Count == 0)
                {
                    unt = null;
                }

                if (soCode.Count == 0)
                {
                    soCode = null;
                }
                sPol.Brnch = br;
                sPol.Unt = unt;
                sPol.SOCode = soCode;
                if (!string.IsNullOrEmpty(clientId))
                {
                    sPol.CliemtId = clientId;

                }


                sPol.Name = name;


                sPol.IdType = idType;
                sPol.IdNo = idNo;
                sPol.Contact = contact;
                //List<string> sol = new List<string>();
                //sol.Add(soCode);
                //ListItem it = new ListItem(soCode);
                //sPol.SOCode = sol ;

                if (string.IsNullOrEmpty(idNo) && string.IsNullOrEmpty(idType) && string.IsNullOrEmpty(idType) && br == null && unt == null && soCode == null && string.IsNullOrEmpty(clientId) && string.IsNullOrEmpty(name) && string.IsNullOrEmpty(contact))
                {
                    MassageSummery("IIS516");
                    int[] sus = { 1 };
                    DataTable dt = new DataTable();
                    UCGrid1.SetDataSource(dt, sus);
                    UCGridButtons1.Visible = false;
                    
                }
                else
                {
                    UCGridButtons1.Visible = true;
                    //List<Policy> resPolicy = cusFac.SearchPolicies(idNo, idType, name, clientId, contact, brnchId, unId, soCode);


                    int fg = 0;
                    if (!string.IsNullOrEmpty(name))
                    {
                        if (br == null || unt == null)
                        {
                            fg = 1;
                        }

                    }

                    if (fg == 0)
                    {
                        switch (type)
                        {
                            case "Client":
                                //ClientSource(ContactSearch cSearch)
                                int[] cusNos = { 1, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 };
                                DataTable dt = ClientSource(sPol, pageNo);
                                

                                UCGrid1.styleList = new List<GridViewStyle>
                                    {
                                         new GridViewStyle{
                                            ColumnId = "Id No",
                                            ColumnHeader = "Id No",
                                            ColumnWidth = 70 },
                                         new GridViewStyle{
                                            ColumnId = "Name",
                                            ColumnHeader = "Name",
                                            ColumnWidth = 160},
                                         new GridViewStyle{
                                            ColumnId = "SO Code",
                                            ColumnHeader = "SO Code",
                                            ColumnWidth = 70 },
                                         new GridViewStyle{
                                            ColumnId = "Client Id",
                                            ColumnHeader = "Client Id",
                                            ColumnWidth = 70 },
                                         new GridViewStyle{
                                            ColumnId = "Last Updated",
                                            ColumnHeader = "Last Updated",
                                            ColumnWidth = 80,
                                            Align = "Right"}
                                    };
                                
                                if (dt.Rows.Count > 0)
                                {
                                    UCGrid1.SetDataSource(dt, cusNos);
                                }
                                else
                                {
                                    UCGrid1.SetDataSource(dt, cusNos);
                                    UCGridButtons1.Visible = false;
                                    MassageSummery("IIS504");
                                }
                                break;
                            case "Policy":
                                List<Policy> resPolicy = cusFac.SearchPolicies(sPol, pageNo);
                                if (resPolicy.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                                {
                                    UCGrid1.IsLastPage = (cusFac.SearchPolicies(sPol, pageNo + 1)).Count > 0 ? false : true;
                                }

                                if (resPolicy.Count > 0)
                                {
                                    UCGridButtons1.SetCredentials(ValidateUserCredentials(Request.Url.LocalPath));

                                }
                                else
                                {
                                    UCGridButtons1.Visible = false;
                                    MassageSummery("IIS504");
                                }

                                //int[] hideNo = { 1, 6, 8, 9, 10, 12, 13, 14, 15, 18 };
                                int[] hideNo = { 1, 11, 12, 13, 14, 15,16,17, 18 };

                                UCGrid1.styleList = new List<GridViewStyle>
                                {
                                     new GridViewStyle{
                                        ColumnId = "ContractId",
                                        ColumnHeader = "Policy No",
                                        ColumnWidth = 130 },
                                     new GridViewStyle{
                                        ColumnId = "CustomerId",
                                        ColumnHeader = "Client Id",
                                        ColumnWidth = 130 },
                                     new GridViewStyle{
                                        ColumnId = "PlanCode",
                                        ColumnHeader = "Plan CD",
                                        ColumnWidth = 130,
                                        Align = "Right"},
                                     new GridViewStyle{
                                        ColumnId = "SOCode",
                                        ColumnHeader = "SO Code",
                                        ColumnWidth = 130 },
                                     new GridViewStyle{
                                        ColumnId = "PolicyState",
                                        ColumnHeader = "Policy State",
                                        ColumnWidth = 130 },
                                     new GridViewStyle{
                                        ColumnId = "Premium",
                                        ColumnHeader = "Premium(Rs)",
                                        ColumnWidth = 130,
                                        Align = "Right"},
                                     new GridViewStyle{
                                        ColumnId = "CollectionDue",
                                        ColumnHeader = "Collection Due(Rs)",
                                        ColumnWidth = 130,
                                        Align = "Right"},
                                     new GridViewStyle{
                                        ColumnId = "LastUpdatedDate",
                                        ColumnHeader = "Last Updated Date",
                                        ColumnWidth = 130,
                                        Align = "right"},
                                     new GridViewStyle{
                                        ColumnId = "DueDate",
                                        ColumnHeader = "Due Date",
                                        ColumnWidth = 130,
                                        Align = "right"},


                                };


                                UCGrid1.SetDataSource(resPolicy, hideNo);

                                break;
                            case "Prospect":
                                int fl = 0;
                                if (!string.IsNullOrEmpty(txtClientId.Text) && soCode == null)
                                {
                                    fl = 1;

                                }
                                else if (!string.IsNullOrEmpty(txtClientId.Text) && soCode != null)
                                {

                                    sPol.CliemtId = txtClientId.Text;

                                }


                                if (fl == 0)
                                {
                                    DataTable dd = Source(sPol, pageNo);
                                    

                                    UCGrid1.styleList = new List<GridViewStyle>
                                    {
                                         new GridViewStyle{
                                            ColumnId = "Id No",
                                            ColumnHeader = "Id No",
                                            ColumnWidth = 130 },
                                         new GridViewStyle{
                                            ColumnId = "Prospect Id",
                                            ColumnHeader = "Prospect Id",
                                            ColumnWidth = 130 },
                                         new GridViewStyle{
                                            ColumnId = "Name",
                                            ColumnHeader = "Name",
                                            ColumnWidth = 130
                                         },
                                         new GridViewStyle{
                                            ColumnId = "SO Code",
                                            ColumnHeader = "SO Code",
                                            ColumnWidth = 130 },
                                         new GridViewStyle{
                                            ColumnId = "Other Name",
                                            ColumnHeader = "Other Name",
                                            ColumnWidth = 130 },
                                         new GridViewStyle{
                                            ColumnId = "Last Updated",
                                            ColumnHeader = "Last Updated",
                                            ColumnWidth = 130,
                                            Align = "Right"}


                                    };


                                    int[] hideNos = { 1, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 };
                                    if (dd.Rows.Count > 0)
                                    {

                                        UCGrid1.SetDataSource(Source(sPol, pageNo), hideNos);
                                    }
                                    else
                                    {
                                        UCGrid1.SetDataSource(Source(sPol, pageNo), hideNos);
                                        UCGridButtons1.Visible = false;
                                        MassageSummery("IIS504");
                                    }


                                }
                                else
                                {
                                    MassageSummery("IIS512");
                                    //lblError.Text = "Please select a SO code.";
                                }
                                //List<Prospect> resProspect = cusFac.SearchProspects(sPol);
                                break;
                            case "Suspect":
                                //List<Prospect> resProspect = cusFac.SearchProspects(sPol);
                                DataTable d = SuspectSource(sPol, pageNo);
                                

                                UCGrid1.styleList = new List<GridViewStyle>
                                    {
                                         new GridViewStyle{
                                            ColumnId = "Id No",
                                            ColumnHeader = "Id No",
                                            ColumnWidth = 130 },
                                         new GridViewStyle{
                                            ColumnId = "Name",
                                            ColumnHeader = "Name",
                                            ColumnWidth = 130
                                         },
                                         new GridViewStyle{
                                            ColumnId = "SO Code",
                                            ColumnHeader = "SO Code",
                                            ColumnWidth = 130 },
                                         new GridViewStyle{
                                            ColumnId = "Other Name",
                                            ColumnHeader = "Other Name",
                                            ColumnWidth = 130 },
                                         new GridViewStyle{
                                            ColumnId = "Last Updated",
                                            ColumnHeader = "Last Updated",
                                            ColumnWidth = 130,
                                            Align = "Right"}


                                    };

                                if (d.Rows.Count > 0)
                                {
                                    int[] susNos = { 1, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,25,26 };
                                    UCGrid1.SetDataSource(SuspectSource(sPol, pageNo), susNos);
                                }
                                else
                                {
                                    int[] susNos = { 1, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,25,26 };
                                    UCGrid1.SetDataSource(SuspectSource(sPol, pageNo), susNos);
                                    UCGridButtons1.Visible = false;
                                    MassageSummery("IIS504");
                                }

                                break;
                            default:
                                break;
                        }
                    }
                    else
                    {
                        MassageSummery("IIS515");
                    }
                }
            }
            catch (Exception)
            {

            }

        }




        private DataTable Source(ContactSearch cSearch,int pageNo)
        {
            try
            {
                StructureFacade fac = new StructureFacade();
                CustomerFacade cusFac = new CustomerFacade();

                DataTable usrTable = new DataTable();
                DataRow newRow;

                usrTable.Columns.Add("Id No");
                usrTable.Columns.Add("Prospect Id");
                usrTable.Columns.Add("SO Code");
                usrTable.Columns.Add("Name");
                usrTable.Columns.Add("Other Name");
                usrTable.Columns.Add("Last Updated");
                //hidden columns
                usrTable.Columns.Add("Last Name");
                usrTable.Columns.Add("Status");
                usrTable.Columns.Add("Id Type");
                usrTable.Columns.Add("DOB");
                usrTable.Columns.Add("Marital Status");
                usrTable.Columns.Add("Profession");
                usrTable.Columns.Add("Country");
                usrTable.Columns.Add("State");
                usrTable.Columns.Add("Home Telephone");
                usrTable.Columns.Add("Office Telephone");
                usrTable.Columns.Add("Email");
                usrTable.Columns.Add("Mobile");
                usrTable.Columns.Add("Cust Id");
                usrTable.Columns.Add("Address1");
                usrTable.Columns.Add("Address2");
                usrTable.Columns.Add("Gender");
                usrTable.Columns.Add("City");
                usrTable.Columns.Add("Remarks");

                string idNo = "";
                string name = "";
                string otherName = "";
                string lstUpdatedTime = "";
                string lstName = "";
                string status;
                string idType;
                string dob;
                string maritalState = "";
                string profession = "";
                string country = "";
                string state = "";
                string homePhone = "";
                string officePhone = "";
                string email = "";
                string mobile = "";
                string address1 = "";
                string gender = "";
                string address2 = "";
                string city = "";

                string remarks = "";

                List<Customer> list = cusFac.SearchProspects(cSearch, pageNo);
                if (list.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                {
                    UCGrid1.IsLastPage = (cusFac.SearchProspects(cSearch, pageNo + 1)).Count > 0 ? false : true;
                }

                foreach (Customer prosp in list)
                {

                    if (prosp != null && !string.IsNullOrEmpty(prosp.CustomerId))
                    {
                        //Customer cus = cusFac.GetCustomer(prosp.CustomerId);
                        //Prospect pr = cusFac.GetProspForCustomer(prosp.CustomerId);

                        idNo = prosp.IdNumber;
                        name = prosp.Title + " " + prosp.FirstName + " " + prosp.LastName;
                        otherName = prosp.OtherName;
                        lstUpdatedTime = prosp.LastUpdated;
                        lstName = prosp.LastName;
                        status = prosp.Status;
                        idType = prosp.IdType;
                        dob = prosp.DOB;
                        maritalState = prosp.MaritalStatus;
                        profession = prosp.Profession;
                        country = prosp.Country;
                        state = prosp.State;
                        homePhone = prosp.HomeTelephone;
                        officePhone = prosp.OfficialTelephone;
                        email = prosp.EMail;
                        mobile = prosp.Mobile;
                        address1 = prosp.Address1;
                        address2 = prosp.Address2;
                        city = prosp.City;
                        gender = prosp.Gender;
                        remarks = prosp.Remarks;



                        newRow = usrTable.NewRow();
                        newRow[0] = idNo;
                        newRow[1] = prosp.CustomerId;
                        newRow[2] = prosp.SoCode;
                        //cusFac.GetSOForCustomer(prosp.CustomerId);
                        newRow[3] = name;
                        newRow[4] = otherName;
                        newRow[5] = lstUpdatedTime;
                        newRow[6] = lstName;
                        newRow[7] = status.ToString();
                        newRow[8] = idType.ToString();
                        newRow[9] = dob;
                        newRow[10] = maritalState;
                        newRow[11] = profession;
                        newRow[12] = country;
                        newRow[13] = state;
                        newRow[14] = homePhone;
                        newRow[15] = officePhone;
                        newRow[16] = email;
                        newRow[17] = mobile;
                        newRow[18] = prosp.CustomerId;
                        newRow[19] = address1;
                        newRow[20] = address2;
                        newRow[21] = gender;
                        newRow[22] = city;
                        newRow[23] = remarks;

                        usrTable.Rows.Add(newRow);
                    }



                }

                if (usrTable.Rows.Count > 0)
                {
                    UCGridButtons1.SetCredentials(ValidateUserCredentials(Request.Url.LocalPath));
                }
                else
                {
                    UCGridButtons1.Visible = false;
                    MassageSummery("IIS504");
                }


                return usrTable;
            }
            catch (Exception)
            {
                return null;
            }   
            
        }




        private DataTable SuspectSource(ContactSearch cSearch,int pageNo)
        {
            try
            {
                StructureFacade fac = new StructureFacade();
                CustomerFacade cusFac = new CustomerFacade();

                DataTable usrTable = new DataTable();
                DataRow newRow;

                usrTable.Columns.Add("Id No");
                usrTable.Columns.Add("SO Code");
                usrTable.Columns.Add("Name");
                usrTable.Columns.Add("Other Name");
                usrTable.Columns.Add("Last Updated");
                //hidden columns
                usrTable.Columns.Add("Last Name");
                usrTable.Columns.Add("Status");
                usrTable.Columns.Add("Id Type");
                usrTable.Columns.Add("DOB");
                usrTable.Columns.Add("Marital Status");
                usrTable.Columns.Add("Profession");
                usrTable.Columns.Add("Country");
                usrTable.Columns.Add("State");
                usrTable.Columns.Add("Home Telephone");
                usrTable.Columns.Add("Office Telephone");
                usrTable.Columns.Add("Email");
                usrTable.Columns.Add("Mobile");
                usrTable.Columns.Add("Cust Id");
                usrTable.Columns.Add("Address1");
                usrTable.Columns.Add("Address2");
                usrTable.Columns.Add("Gender");
                usrTable.Columns.Add("City");
                usrTable.Columns.Add("Remarks");
                usrTable.Columns.Add("PrefPlace");
                usrTable.Columns.Add("PrefNo");

                string idNo = "";
                string name = "";
                string otherName = "";
                string lstUpdatedTime = "";
                string lstName = "";
                string status;
                string idType;
                string dob;
                string maritalState = "";
                string profession = "";
                string country = "";
                string state = "";
                string homePhone = "";
                string officePhone = "";
                string email = "";
                string mobile = "";
                string address1 = "";
                string gender = "";
                string address2 = "";
                string city = "";
                string remarks = "";

                List<Customer> list = cusFac.SearchSuspects(cSearch, pageNo);
                if (list.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                {
                    UCGrid1.IsLastPage = (cusFac.SearchSuspects(cSearch, pageNo + 1)).Count > 0 ? false : true;
                }

                foreach (Customer cus in list)
                {

                    if (cus != null && !string.IsNullOrEmpty(cus.CustomerId))
                    {
                        //Customer cus = cusFac.GetCustomer(susp.CustomerId);
                        idNo = cus.IdNumber;
                        name = cus.Title + " " + cus.FirstName + " " + cus.LastName;
                        otherName = cus.OtherName;
                        lstUpdatedTime = cus.LastUpdated;
                        lstName = cus.LastName;
                        status = cus.Status;
                        idType = cus.IdType;
                        dob = cus.DOB;
                        maritalState = cus.MaritalStatus;
                        profession = cus.Profession;
                        country = cus.Country;
                        state = cus.State;
                        homePhone = cus.HomeTelephone;
                        officePhone = cus.OfficialTelephone;
                        email = cus.EMail;
                        mobile = cus.Mobile;
                        address1 = cus.Address1;
                        address2 = cus.Address2;
                        gender = cus.Gender;
                        city = cus.City;
                        remarks = cus.Remarks;

                        newRow = usrTable.NewRow();
                        newRow[0] = idNo;
                        newRow[1] = cus.SoCode;
                        //cusFac.GetSOForCustomer(cus.CustomerId);
                        newRow[2] = name;
                        newRow[3] = otherName;
                        newRow[4] = lstUpdatedTime;
                        newRow[5] = lstName;
                        newRow[6] = status.ToString();
                        newRow[7] = idType.ToString();
                        newRow[8] = dob;
                        newRow[9] = maritalState;
                        newRow[10] = profession;
                        newRow[11] = country;
                        newRow[12] = state;
                        newRow[13] = homePhone;
                        newRow[14] = officePhone;
                        newRow[15] = email;
                        newRow[16] = mobile;
                        newRow[17] = cus.CustomerId;
                        newRow[18] = address1;
                        newRow[19] = address2;
                        newRow[20] = gender;
                        newRow[21] = city;
                        newRow[22] = remarks;
                        newRow[23] = cus.PrefPlace;
                        newRow[24] = cus.PrefNo;

                        usrTable.Rows.Add(newRow);
                    }



                }

                if (usrTable.Rows.Count > 0)
                {
                    UCGridButtons1.SetCredentials(ValidateUserCredentials(Request.Url.LocalPath));
                }
                else
                {
                    MassageSummery("IIS504");
                    UCGridButtons1.Visible = false;
                }

                return usrTable;
            }
            catch (Exception)
            {
                return null;
            }
            
        }





        private DataTable ClientSource(ContactSearch cSearch,int pageNo)
        {
            try
            {
                StructureFacade fac = new StructureFacade();
                CustomerFacade cusFac = new CustomerFacade();

                DataTable usrTable = new DataTable();
                DataRow newRow;

                usrTable.Columns.Add("Id No");
                usrTable.Columns.Add("Client Id");
                usrTable.Columns.Add("SO Code");
                usrTable.Columns.Add("Name");
                usrTable.Columns.Add("Other Name");
                usrTable.Columns.Add("Last Updated Date");

                //hidden columns
                usrTable.Columns.Add("Last Name");
                usrTable.Columns.Add("Status");
                usrTable.Columns.Add("Id Type");
                usrTable.Columns.Add("DOB");
                usrTable.Columns.Add("Marital Status");
                usrTable.Columns.Add("Profession");
                usrTable.Columns.Add("Country");
                usrTable.Columns.Add("State");
                usrTable.Columns.Add("Home Telephone");
                usrTable.Columns.Add("Office Telephone");
                usrTable.Columns.Add("Email");
                usrTable.Columns.Add("Mobile");
                usrTable.Columns.Add("Cust Id");
                usrTable.Columns.Add("Address1");
                usrTable.Columns.Add("Address2");
                usrTable.Columns.Add("Gender");
                usrTable.Columns.Add("City");
                usrTable.Columns.Add("Remarks");

                string idNo = "";
                string clientId = "";
                string name = "";
                string otherName = "";
                string lstUpdatedTime = "";
                string lstName = "";
                string status;
                string idType;
                string dob;
                string maritalState = "";
                string profession = "";
                string country = "";
                string state = "";
                string homePhone = "";
                string officePhone = "";
                string email = "";
                string mobile = "";
                string address1 = "";
                string gender = "";
                string address2 = "";
                string city = "";

                string remarks = "";

                UCGrid1.SetText(pageNo.ToString());



                //foreach (Policy pol in cusFac.SearchPolicies(cSearch,pageNo))
                //{
                //    if (pol != null && !string.IsNullOrEmpty(pol.CustomerId))
                //    {

                //    }
                //}

                List<Customer> list = cusFac.SearchCustomers(cSearch, pageNo);
                if (list.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                {
                    UCGrid1.IsLastPage = (cusFac.SearchCustomers(cSearch, pageNo + 1)).Count > 0 ? false : true;
                }

                foreach (Customer cus in list)
                {

                    if (cus != null && !string.IsNullOrEmpty(cus.CustomerId))
                    {
                        idNo = cus.IdNumber;
                        clientId = cus.CustomerId;
                        name = cus.Title + " " + cus.FirstName + " " + cus.LastName;
                        otherName = cus.OtherName;
                        lstUpdatedTime = cus.LastUpdated;
                        lstName = cus.LastName;
                        status = cus.Status;
                        idType = cus.IdType;
                        dob = cus.DOB;
                        maritalState = cus.MaritalStatus;
                        profession = cus.Profession;
                        country = cus.Country;
                        state = cus.State;
                        homePhone = cus.HomeTelephone;
                        officePhone = cus.OfficialTelephone;
                        email = cus.EMail;
                        mobile = cus.Mobile;
                        address1 = cus.Address1;
                        address2 = cus.Address2;
                        city = cus.City;
                        gender = cus.Gender;
                        remarks = cus.Remarks;



                        newRow = usrTable.NewRow();
                        newRow[0] = idNo;
                        newRow[1] = clientId;
                        newRow[2] = cus.SoCode;
                        //cusFac.GetSOForCustomer(clientId);
                        newRow[3] = name;
                        newRow[4] = otherName;
                        newRow[5] = lstUpdatedTime;
                        newRow[6] = lstName;
                        newRow[7] = status.ToString();
                        newRow[8] = idType.ToString();
                        newRow[9] = dob;
                        newRow[10] = maritalState;
                        newRow[11] = profession;
                        newRow[12] = country;
                        newRow[13] = state;
                        newRow[14] = homePhone;
                        newRow[15] = officePhone;
                        newRow[16] = email;
                        newRow[17] = mobile;
                        newRow[18] = address1;
                        newRow[19] = address2;
                        newRow[20] = gender;
                        newRow[21] = city;
                        newRow[22] = remarks;

                        usrTable.Rows.Add(newRow);
                    }



                }

                if (usrTable.Rows.Count > 0)
                {
                    UCGridButtons1.SetCredentials(ValidateUserCredentials(Request.Url.LocalPath));
                }
                else
                {
                    MassageSummery("IIS504");
                    UCGridButtons1.Visible = false;
                }


                return usrTable;
            }
            catch (Exception)
            {
                return null;
            }
            
        }





        public void FillDropDown(string type)
        {
            

            try
            {
                StructureFacade fac = new StructureFacade();
                string curUser = User.Identity.Name;
                if (!string.IsNullOrEmpty(curUser))
                {
                    int[] strUser = fac.GetStructureForUser(curUser);

                    UserInformation ui = fac.GetUserInformation(User.Identity.Name);
                    

                    int usrType = strUser[0];
                    int node = strUser[1];

                    ListItem itt = new ListItem("Select...", "-1");

                    if (usrType == 1)
                    {
                        //HQ
                        switch (type)
                        {
                            case "Branch":
                                brnch = fac.GetBranches("BranchName ASC");
                                Branch brsel = new Branch();
                                brsel.BranchId = -1;
                                brsel.BranchName = "Select...";
                                brnch.Insert(0, brsel);
                                Branch brsel1 = new Branch();
                                brsel1.BranchId = -2;
                                brsel1.BranchName = "Select All";
                                brnch.Insert(1, brsel1);

                                branchDropDownList.DataSource = brnch;
                                branchDropDownList.DataTextField = "BranchName";
                                branchDropDownList.DataValueField = "BranchId";
                                branchDropDownList.DataBind();
                                break;

                            case "Unit":
                                if (branchDropDownList.SelectedValue != "-1")
                                {
                                    un = fac.GetUnitsInBranch(Convert.ToInt32(branchDropDownList.SelectedValue));
                                    SalesOneUnit unSel = new SalesOneUnit();
                                    unSel.UnitId = -1;
                                    unSel.Name = "Select...";
                                    un.Insert(0, unSel);
                                    SalesOneUnit unSel1 = new SalesOneUnit();
                                    unSel1.UnitId = -2;                               
                                    unSel1.Name = "Select All";
                                    un.Insert(1, unSel1);
                                    unitDropDownList.DataSource = un;
                                    unitDropDownList.DataTextField = "Name";
                                    unitDropDownList.DataValueField = "UnitId";
                                    unitDropDownList.DataBind();
                                }
                                else
                                {
                                    unitDropDownList.Items.Clear();
                                    unitDropDownList.Items.Insert(0,itt);
                                }
                                break;


                            case "SO":
                                if (unitDropDownList.SelectedValue != "-1")
                                {
                                    so = fac.GetSOsInUnit(Convert.ToInt32(unitDropDownList.SelectedValue));
                                    //soCodeDropDownList.DataSource = so;
                                    string srl = "Select...";
                                    string sra = "Select All";
                                    so.Insert(0, srl);
                                    so.Insert(1, sra);
                                    soCodeDropDownList.DataSource = so;
                                    //soCodeDropDownList.DataTextField = "BranchName";
                                    //drpBranch.DataValueField = "BranchId";
                                    soCodeDropDownList.DataBind();
                                }
                                else
                                {
                                    soCodeDropDownList.Items.Clear();
                                    soCodeDropDownList.Items.Insert(0, itt);
                                }
                                break;
                            default:
                                break;
                        }
                    }
                    else if (usrType == 2)
                    {
                        //Group
                        switch (type)
                        {
                            case "Branch":
                                brnch = fac.GetBranchesForGroup(node);
                                Branch brsel = new Branch();
                                brsel.BranchId = -1;
                                brsel.BranchName = "Select...";
                                brnch.Insert(0, brsel);
                                Branch brsel1 = new Branch();
                                brsel1.BranchId = -2;
                                brsel1.BranchName = "Select All";
                                brnch.Insert(1, brsel1);
                                branchDropDownList.DataSource = brnch;
                                branchDropDownList.DataTextField = "BranchName";
                                branchDropDownList.DataValueField = "BranchId";
                                branchDropDownList.DataBind();
                                branchDropDownList.SelectedValue = "-2";
                                break;

                            case "Unit":
                                if (branchDropDownList.SelectedValue != "-1")
                                {
                                    //if (branchDropDownList.SelectedValue != "-2")
                                    //{
                                        un = fac.GetUnitsInBranch(Convert.ToInt32(branchDropDownList.SelectedValue));
                                        SalesOneUnit unSel = new SalesOneUnit();
                                        unSel.UnitId = -1;
                                        unSel.Name = "Select...";
                                        un.Insert(0, unSel);
                                        SalesOneUnit unSel1 = new SalesOneUnit();
                                        unSel1.UnitId = -2;
                                        unSel1.Name = "Select All";
                                        un.Insert(1, unSel1);
                                        unitDropDownList.DataSource = un;
                                        unitDropDownList.DataTextField = "Name";
                                        unitDropDownList.DataValueField = "UnitId";
                                        unitDropDownList.DataBind();
                                        unitDropDownList.SelectedValue = "-2"; 
                                    //}
                                    //else
                                    //{

                                    //}
                                    
                                }
                                else
                                {
                                    unitDropDownList.Items.Clear();
                                    unitDropDownList.Items.Insert(0, itt);
                                }
                                break;


                            case "SO":
                                if (unitDropDownList.SelectedValue != "-1")
                                {
                                    so = fac.GetSOsInUnit(Convert.ToInt32(unitDropDownList.SelectedValue));
                                    //soCodeDropDownList.DataSource = so;
                                    string srl = "Select...";
                                    string sra = "Select All";
                                    so.Insert(0, srl);
                                    so.Insert(1, sra);
                                    soCodeDropDownList.DataSource = so;
                                    //soCodeDropDownList.DataTextField = "BranchName";
                                    //drpBranch.DataValueField = "BranchId";
                                    soCodeDropDownList.DataBind();
                                }
                                else
                                {
                                    soCodeDropDownList.Items.Clear();
                                    soCodeDropDownList.Items.Insert(0, itt);
                                }
                                break;
                            default:
                                break;
                        }

                    }
                    else if (usrType == 3)
                    {
                        //Branch
                        switch (type)
                        {
                            case "Branch":
                                Branch brnch = fac.GetBranch(node);
                                ListItem it = new ListItem(brnch.BranchName, brnch.BranchId.ToString());
                                branchDropDownList.Items.Add(it);
                                //branchDropDownList.DataTextField = brnch.BranchName;
                                //branchDropDownList.DataValueField = brnch.BranchId.ToString();
                                //branchDropDownList.DataBind();
                                branchDropDownList.Enabled = false;
                                break;

                            case "Unit":
                                if (branchDropDownList.SelectedValue != "-1")
                                {
                                    un = fac.GetUnitsInBranch(Convert.ToInt32(branchDropDownList.SelectedValue));
                                    SalesOneUnit unSel = new SalesOneUnit();
                                    unSel.UnitId = -1;
                                    unSel.Name = "Select...";
                                    un.Insert(0, unSel);
                                    SalesOneUnit unSel1 = new SalesOneUnit();
                                    unSel1.UnitId = -2;
                                    unSel1.Name = "Select All";
                                    un.Insert(1, unSel1);
                                    unitDropDownList.DataSource = un;
                                    unitDropDownList.DataTextField = "Name";
                                    unitDropDownList.DataValueField = "UnitId";
                                    unitDropDownList.DataBind();
                                    unitDropDownList.SelectedValue = "-2";
                                }
                                else
                                {
                                    unitDropDownList.Items.Clear();
                                    unitDropDownList.Items.Insert(0, itt);
                                }
                                break;


                            case "SO":
                                if (unitDropDownList.SelectedValue != "-1")
                                {
                                    so = fac.GetSOsInUnit(Convert.ToInt32(unitDropDownList.SelectedValue));
                                    //soCodeDropDownList.DataSource = so;
                                    string srl = "Select...";
                                    string sra = "Select All";
                                    so.Insert(0, srl);
                                    so.Insert(1, sra);
                                    soCodeDropDownList.DataSource = so;
                                    //soCodeDropDownList.DataTextField = "BranchName";
                                    //drpBranch.DataValueField = "BranchId";
                                    soCodeDropDownList.DataBind();
                                }
                                else
                                {
                                    soCodeDropDownList.Items.Clear();
                                    soCodeDropDownList.Items.Insert(0, itt);
                                }
                                break;
                            default:
                                break;
                        }
                    }
                    else if (usrType == 4)
                    {
                        //unit
                        switch (type)
                        {
                            case "Branch":

                                Branch brnch1 = fac.GetBranchForUnit(node);
                                ListItem itb = new ListItem(brnch1.BranchName, brnch1.BranchId.ToString());
                                branchDropDownList.Items.Add(itb);
                                //branchDropDownList.DataTextField = "BranchName";
                                //branchDropDownList.DataValueField = "BranchId";
                                //branchDropDownList.DataBind();
                                branchDropDownList.Enabled = false;
                                break;

                            case "Unit":
                                if (branchDropDownList.SelectedValue != "-1")
                                {
                                    SalesOneUnit un = fac.GetUnit(node);
                                    ListItem it = new ListItem(un.Name, un.UnitId.ToString());
                                    unitDropDownList.Items.Add(it);
                                    unitDropDownList.Enabled = false;
                                }
                                else
                                {
                                    unitDropDownList.Items.Clear();
                                    unitDropDownList.Items.Insert(0, itt);
                                }
                                break;


                            case "SO":
                                if (unitDropDownList.SelectedValue != "-1")
                                {
                                    so = fac.GetSOsInUnit(Convert.ToInt32(unitDropDownList.SelectedValue));
                                    //so.com
                                    //= fac.GetSOsInUnit(Convert.ToInt32(unitDropDownList.SelectedValue))
                                    //soCodeDropDownList.DataSource = so;
                                    string srl = "Select...";
                                    string sra = "Select All";
                                    so.Insert(0, srl);
                                    so.Insert(1, sra);
                                    soCodeDropDownList.DataSource = so;
                                    //soCodeDropDownList.DataTextField = "BranchName";
                                    //drpBranch.DataValueField = "BranchId";
                                    soCodeDropDownList.DataBind();
                                }
                                else
                                {
                                    soCodeDropDownList.Items.Clear();
                                    soCodeDropDownList.Items.Insert(0, itt);
                                }
                                break;
                            default:
                                break;
                        }
                    }
                    
                }
                else
                {
                    Response.Redirect("~/Login.aspx");
                }

                
            }
            catch (Exception ex)
            {

                //Response.Write(ex.Message);
            }

            


            
        }

        protected void unitDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillDropDown("SO");
        }

        protected void contractNoTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        #region IDataPage Members

        public void Create()
        {
            
        }

        public void View()
        {
            try
            {
                if (Session["firstRow"] != null)
                {

                    GridViewRow row = ((GridViewRow)Session["firstRow"]);
                    //GridViewRow row = ((GridView)UCGrid1.FindControl("grdUsers")).SelectedRow;


                    if (rdoContactType.SelectedIndex == 0)
                    {
                        CustomerFacade cFc = new CustomerFacade();
                        Customer cust = cFc.GetCustomer(row.Cells[3].Text);

                        if (Session["client"] == null)
                        {
                            Session["client"] = cust;
                            Session["so"] = row.Cells[4].Text;
                        }

                    }
                    else if (rdoContactType.SelectedIndex == 1)
                    {
                        //Session["policyObj"] = null;
                        if (Session["policyObj"] == null)
                        {
                            Session["policyObj"] = row.Cells[2].Text;
                        }
                    }
                    else if (rdoContactType.SelectedIndex == 2)
                    {
                        //Session["ProspObj"] = null;
                        if (Session["ProspObj"] == null)
                        {
                            Session["ProspObj"] = row.Cells[3].Text;
                        }
                    }
                    else if (rdoContactType.SelectedIndex == 3)
                    {
                        if (Session["SuspObj"] == null)
                        {
                            Session["SuspObj"] = row;
                        }
                    }
                    Session.Remove("firstRow");
                }



                if (rdoContactType.SelectedIndex == 0)
                {
                    if (Session["client"] != null)
                    {
                        Response.Redirect("~/ContactManagement/ClientDetails.aspx?t=p");
                        Session.Remove("policyObj");

                    }
                    else
                    {
                        //lblError.Text = "Please select a policy.";
                        MassageSummery("IIS503");
                    }

                }
                if (rdoContactType.SelectedIndex == 1)
                {
                    if (Session["policyObj"] != null)
                    {
                        Response.Redirect("~/ContactManagement/PolicyDetails.aspx?con=" + Session["policyObj"].ToString() + "&p=p");
                        Session.Remove("policyObj");

                    }
                    else
                    {
                        //lblError.Text = "Please select a policy.";
                        MassageSummery("IIS503");
                    }

                }
                else if (rdoContactType.SelectedIndex == 2)
                {
                    if (Session["ProspObj"] != null)
                    {
                        Response.Redirect("~/ContactManagement/ProspectDetails.aspx?id=" + Session["ProspObj"].ToString());
                        Session.Remove("ProspObj");

                    }
                    else
                    {
                        //lblError.Text = "Please select a prospect.";
                        MassageSummery("IIS503");
                    }
                }
                else if (rdoContactType.SelectedIndex == 3)
                {
                    if (Session["SuspObj"] != null)
                    {
                        Response.Redirect("~/ContactManagement/SuspectDetails.aspx");
                    }
                    else
                    {
                        //lblError.Text = "Please select a suspect.";
                        MassageSummery("IIS503");
                    }
                }

            }
            catch (Exception ex)
            {

            }
            
        }

        public void Update()
        {
            
        }

        public void Delete()
        {
            
        }

        public void Cancel()
        {
            
        }

        public void GetId(GridViewRow row)
        {
            try
            {
                if (rdoContactType.SelectedIndex == 0)
                {
                    CustomerFacade cFc = new CustomerFacade();
                    Customer cust = cFc.GetCustomer(row.Cells[3].Text);

                    Session["client"] = cust;
                    Session["so"] = row.Cells[4].Text;

                }
                else if (rdoContactType.SelectedIndex == 1)
                {
                    // Suren Manawatta.
                    // 2012-07-12.
                    // row.Cells[3].Text was the previous code and it direct to "Client Id"(cell 3) in the
                    // grid view but it need to be direct to "Policy No" which is cell 2.
                    Session["policyObj"] = row.Cells[2].Text;
                }
                else if (rdoContactType.SelectedIndex == 2)
                {
                    Session["ProspObj"] = row.Cells[3].Text;
                }
                else if (rdoContactType.SelectedIndex == 3)
                {
                    Session["SuspObj"] = row;
                }
            }
            catch (Exception)
            {

            }
            

            
        }

        #endregion

        #region IntTest Members

        public void Next()
        {
            if (UCGrid1.GetText() != "1")
            {
                pageNo = Convert.ToInt32(UCGrid1.GetText()) + 1;
                UCGrid1.SetText(pageNo.ToString());

            }
            else
            {
                pageNo = 2;
                UCGrid1.SetText("2");

            }


            int contType = Convert.ToInt32(rdoContactType.SelectedValue);

            switch (contType)
            {
                case 1:
                    //client
                    getContacts("Client",pageNo);
                    break;
                case 2:
                    //policy
                    getContacts("Policy", pageNo);
                    break;
                case 3:
                    //prospect
                    getContacts("Prospect", pageNo);
                    break;
                case 4:
                    //suspect
                    getContacts("Suspect", pageNo);
                    break;
                default:
                    break;
            }




            //switch (csType)
            //{
            //    case "ack":
            //        SearchAck("ack", pageNo);
            //        break;
            //    case "pep":
            //        SearchAck("pep", pageNo);
            //        break;
            //    default:
            //        break;
            //}

        }

        public void Previous()
        {
            if (UCGrid1.GetText() != "1")
            {
                pageNo = Convert.ToInt32(UCGrid1.GetText()) - 1;
                UCGrid1.SetText(pageNo.ToString());

                int contType = Convert.ToInt32(rdoContactType.SelectedValue);

                switch (contType)
                {
                    case 1:
                        //client
                        getContacts("Client", pageNo);
                        break;
                    case 2:
                        //policy
                        getContacts("Policy", pageNo);
                        break;
                    case 3:
                        //prospect
                        getContacts("Prospect", pageNo);
                        break;
                    case 4:
                        //suspect
                        getContacts("Suspect", pageNo);
                        break;
                    default:
                        break;
                }

                //string csType = string.Empty;
                //if (Request.QueryString["cs"] != null)
                //{
                //    csType = Request.QueryString["cs"].ToString();
                //}


                //switch (csType)
                //{
                //    case "ack":
                //        SearchAck("ack", pageNo);
                //        break;
                //    case "pep":
                //        SearchAck("pep", pageNo);
                //        break;
                //    default:
                //        break;
                //}



            }
        }

        public void Go()
        {

            int p = int.Parse(UCGrid1.GetText());
            if (p != 0)
            {
                try
                {
                    int contType = Convert.ToInt32(rdoContactType.SelectedValue);

                    switch (contType)
                    {
                        case 1:
                            //client
                            getContacts("Client", p);
                            break;
                        case 2:
                            //policy
                            getContacts("Policy", p);
                            break;
                        case 3:
                            //prospect
                            getContacts("Prospect", p);
                            break;
                        case 4:
                            //suspect
                            getContacts("Suspect", p);
                            break;
                        default:
                            break;
                    }

                    pageNo = p;
                }
                catch (Exception)
                {

                    throw;
                }


            }

        }

        #endregion

        protected void rdoContactType_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                UCGrid1.ResetDataSource();
                UCGridButtons1.Visible = false;

                if (rdoContactType.SelectedIndex == 0 || rdoContactType.SelectedIndex == 1)
                {//client or policy
                    lblClientId.Text = "Client ID";
                    lblContId.Text = "Contract No";
                    txtClientId.Visible = true;
                    txtContractNo.Visible = true;
                    lblContId.Visible = true;
                    lblClientId.Visible = true;
                }
                else if (rdoContactType.SelectedIndex == 2)
                {//prospect
                    lblClientId.Text = "Prospect ID(with SO Code)";
                    txtClientId.Visible = true;
                    lblClientId.Visible = true;
                    txtContractNo.Visible = false;
                    lblContId.Visible = false;
                }
                else if (rdoContactType.SelectedIndex == 3)
                {//suspect
                    txtClientId.Visible = false;
                    txtContractNo.Visible = false;
                    lblContId.Visible = false;
                    lblClientId.Visible = false;
                }
            }
            catch (Exception)
            {

            }
           
        }
    }
}
